<script setup>

import {reactive, ref} from "vue";
import {Lock, User} from "@element-plus/icons-vue";
import router from "@/router/router.js";
import {ElMessage} from "element-plus";
import {post} from "@/net/index.js";


const resetPasswordFormRef = ref()
const resetPasswordForm = reactive({
  username: '',
  oldPassword: '',
  newPassword: '',
  newPasswordRepeat: '',
})

const validatorPassword = (rule, value, callback) => {
  if (value === '')
    callback(new Error("请再次输入密码"))
  else if (value !== resetPasswordForm.newPassword)
    callback(new Error("两次输入的密码不一致"))
  else callback()
}

const resetPasswordFormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: ['blur', 'change'] }
  ],
  oldPassword: [
    {required: true, message: '请输入原密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度在 6 到 10 个字符', trigger: ['blur', 'change']}
  ],
  newPassword: [
    {required: true, message: '请输入新密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度在 6 到 10 个字符', trigger: ['blur', 'change']}
  ],
  newPasswordRepeat: [
    {validator: validatorPassword, trigger: ['blur']},
  ]
}

const resetPassword = () => {
  resetPasswordFormRef.value.validate((valid) => {
    if (valid) {
      post('/api/user/resetPassword',
          {...resetPasswordForm},
          () => {
            ElMessage.success('重置密码成功')
            router.push('/')
          }
      )
    }
  })
}
</script>

<template>
  <div style="text-align: center;margin: 0 20px">
    <div style="margin-top: 70px">
      <div style="font-size: 25px;font-weight: bold">重置密码</div>
      <div style="font-size: 13px;color: gray">您需要输入你的原密码和新密码才能设置密码</div>
    </div>
    <div style="margin-top: 50px">
      <el-form :model="resetPasswordForm" ref="resetPasswordFormRef" :rules="resetPasswordFormRules">
        <el-form-item prop="username">
          <el-input type="text" v-model="resetPasswordForm.username" placeholder="用户名">
            <template #prefix>
              <el-icon>
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="oldPassword">
          <el-input type="password" v-model="resetPasswordForm.oldPassword" placeholder="旧密码">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="newPassword">
          <el-input type="password" v-model="resetPasswordForm.newPassword" placeholder="新密码">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="newPasswordRepeat">
          <el-input type="password" v-model="resetPasswordForm.newPasswordRepeat" placeholder="确认密码">
            <template #prefix>
              <el-icon>
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin-top: 70px;margin-bottom: 90px">
      <el-button type="danger" @click="resetPassword()" style="width: 250px" plain>确认重置密码</el-button>
    </div>

  </div>

</template>

<style scoped>

</style>